{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "type object 'Person' has no attribute 'dir'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Input \u001B[1;32mIn [2]\u001B[0m, in \u001B[0;36m<cell line: 9>\u001B[1;34m()\u001B[0m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mclass\u001B[39;00m \u001B[38;5;21;01mPerson\u001B[39;00m:\n\u001B[0;32m      7\u001B[0m     \u001B[38;5;28;01mpass\u001B[39;00m\n\u001B[1;32m----> 9\u001B[0m \u001B[43mPerson\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mdir\u001B[49m()\n",
      "\u001B[1;31mAttributeError\u001B[0m: type object 'Person' has no attribute 'dir'"
     ]
    }
   ],
   "source": [
    "# dir内置函数\n",
    "# 01 __new__ 方法 创建对象时，会被 自动 调用\n",
    "# 02 __init__ 方法 对象被初始化时，会被 自动 调用\n",
    "# 03 __del__ 方法 对象被从内存中销毁前，会被 自动 调用\n",
    "# 04 __str__ 方法 返回对象的描述信息，print 函数输出使用"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom 来了\n"
     ]
    }
   ],
   "source": [
    "# 需求\n",
    "# • 小猫 爱 吃 鱼，小猫 要 喝 水\n",
    "\n",
    "class Cat:\n",
    "\n",
    "    def __init__(self, new_name):\n",
    "\n",
    "        self.name = new_name\n",
    "\n",
    "        print(\"%s 来了\" % self.name)\n",
    "\n",
    "    def __del__(self):  #对象销毁的时候调用\n",
    "        print(\"%s 我去了\" % self.name)\n",
    "\n",
    "\n",
    "    def __str__(self):  #打印对象调用这个\n",
    "\n",
    "        # 必须返回一个字符串\n",
    "        return \"我是小猫[%s]\" % self.name\n",
    "\n",
    "tom=Cat('Tom')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是小猫[Tom]\n"
     ]
    }
   ],
   "source": [
    "print(tom)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom 我去了\n"
     ]
    }
   ],
   "source": [
    "del tom"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}